package lzzy.net.service;


import lzzy.net.entity.Comment;
import lzzy.net.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;
    public Comment findById(Integer id) {
        Optional<Comment> comment = commentRepository.findById(id);
        if (comment.isPresent()) {
            return comment.get();
        }
        return null;
    }
        @CachePut(value = "comment", key = "#id")
                public Comment updateComment(Integer id, String content)
        {
            Comment comment = findById(id);
            comment.setContent(content);
            commentRepository.updateComment(id,content);
            return comment;
        }
    @CacheEvict("comment")
    public void deleteCommentById(Integer id){
        commentRepository.deleteById(id);
    }

    }

